﻿using Dapper;
using Domain.Entity.bs;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class ProductImgRepository : IProductImgRepository
    {
        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //添加货品图片
        public async Task<int> AddProductImg(int procid, string img)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into [dbo].[bs_product_img](proc_id, proc_img)
                                values({procid},'{img}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //根据货品id查询图片
        public async Task<List<ProductImg>> GetImgByPId(int procid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"select * from [dbo].[bs_product_img] where proc_id={procid}";
                return sqlConnection.Query<ProductImg>(sql).ToList();
            }
        }

        //修改货品图片（先删除再添加）
        public async Task<int> UpdateImg(int procid, string img)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"delete from [dbo].[bs_product_img] where proc_id={procid}";
                await sqlConnection.ExecuteAsync(sql);//先删除
                return await AddProductImg(procid,img);//再添加
            }
        }
    }
}
